package com.wxcms.service.impl;

import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
import com.wxcms.domain.WxCpMyConfigStorage;
import com.wxcms.mapper.WxCpDepartDao;
import com.wxcms.mapper.WxCpUserDao;
import com.wxcms.service.BaseService;

import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
 
@Service
 
public class DodemWxServer  extends WxCpServiceImpl  implements BaseService   {
	
	  /**
	   * 全局的是否正在刷新jsapi_ticket的锁
	   */
	  protected final Object globalTicketRefreshLock = new Object();
    
	public WxCpMyConfigStorage getConfig(){
		return (WxCpMyConfigStorage) wxCpConfigStorage;
	}
	public void setWxCpConfigStorage(WxCpMyConfigStorage wxConfigProvider){
		
		super.setWxCpConfigStorage(wxConfigProvider);
	}

	  public String getTicket(boolean forceRefresh) throws WxErrorException {
		  
		  WxCpMyConfigStorage cfg=  (WxCpMyConfigStorage) wxCpConfigStorage  ;
		  
	    if (forceRefresh) {
	    	cfg.expireTicket();
	    }
	    if (cfg.isTicketExpired()) {
	      synchronized (globalTicketRefreshLock) {
	        if (cfg.isTicketExpired()) {
	        //  String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
	          String url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?type=contact";
	          String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
	          JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
	          
	          System.out.println("dddddddddddddddddddddddddddddddddddddddd\n");
	          
	          System.out.println(tmpJsonElement.toString());
	          
	          JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
	          String ticket = tmpJsonObject.get("ticket").getAsString();
	          
	          String group_id = tmpJsonObject.get("group_id").getAsString();
	          
	          int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
	          
	          cfg.updateTicket(ticket,group_id, expiresInSeconds);
	        }
	      }	
	    }
	    return cfg.getTicket();
	  }

	  public WxJsapiSignature createSignature(String url) throws WxErrorException {
	    long timestamp = System.currentTimeMillis() / 1000;
	    String noncestr = RandomUtils.getRandomStr();
	    String ticket = getTicket(false);
	    try {
	      String signature = SHA1.genWithAmple(
	          "group_ticket=" + ticket,
	          "noncestr=" + noncestr,
	          "timestamp=" + timestamp,
	          "url=" + url
	      );
	   
	      WxJsapiSignature jsapiSignature = new WxJsapiSignature();
	      
	      jsapiSignature.setTimestamp(timestamp);
	      jsapiSignature.setNoncestr(noncestr);
	      jsapiSignature.setUrl(url);
	      jsapiSignature.setSignature(signature);
	      
	      System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTT+\n"+ticket+"\nuuuuuuuuuuuu\n"+url);
	      
	      return jsapiSignature;
	      
	    } catch (NoSuchAlgorithmException e) {
	      throw new RuntimeException(e);
	    }
	  }
	 
	
}
